//
//  ZGLGoodsTableViewTitleCell.m
//  Faker
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLGoodsTableViewTitleCell.h"
#import <UIImageView+AFNetworking.h>
#import <UIButton+AFNetworking.h>
#import "ZGLOptionsModel.h"

@interface ZGLGoodsTableViewTitleCell ()

@property (nonatomic, strong) UIButton *upLabel;
@property (nonatomic, strong) UIButton *leftLabel;
@property (nonatomic, strong) UIButton *rightLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *rightTitleLabel;

@property (nonatomic, copy) void(^buttonBlock)(UIButton *button);


@end

@implementation ZGLGoodsTableViewTitleCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.upLabel = [UIButton buttonWithType:UIButtonTypeCustom];
        self.leftLabel = [UIButton buttonWithType:UIButtonTypeCustom];
        self.rightLabel = [UIButton buttonWithType:UIButtonTypeCustom];
        self.titleLabel = [[UILabel alloc] init];
        self.rightTitleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.upLabel];
        [self.contentView addSubview:self.leftLabel];
        [self.contentView addSubview:self.rightLabel];
        [self.leftLabel addSubview:self.titleLabel];
        [self.rightLabel addSubview:self.rightTitleLabel];
    }
    return self;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    self.backgroundColor = foreColor;
    [self.upLabel setFrame:CGRectMake(ScaleX * 15, ScaleY * 15, ScaleX * 345, ScaleY * 60)];
    [self.leftLabel setFrame:CGRectMake(ScaleX * 15, ScaleY * 90, ScaleX * 165, ScaleY * 100)];
    [self.rightLabel setFrame:CGRectMake(ScaleX * 195, ScaleY * 90, ScaleX * 165, ScaleY * 100)];
    [self.titleLabel setFrame:CGRectMake(0, ScaleY * 70, ScaleX * 165, ScaleY * 20)];
    [self.rightTitleLabel setFrame:CGRectMake(0,ScaleY * 70, ScaleX * 165, ScaleY * 20)];
    self.titleLabel.textAlignment = 1;
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    self.rightTitleLabel.textAlignment = 1;
    self.rightTitleLabel.font = [UIFont systemFontOfSize:14];
    [self.upLabel addTarget:self action:@selector(button:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.leftLabel addTarget:self action:@selector(button:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.rightLabel addTarget:self action:@selector(button:) forControlEvents:(UIControlEventTouchUpInside)];
    self.upLabel.tag = 120;
    self.leftLabel.tag = 121;
    self.rightLabel.tag = 122;
}
- (void)button:(UIButton *)button {
    self.buttonBlock(button);
}

- (void)buttonBlock:(void (^)(UIButton *))handle {
    self.buttonBlock = handle;
}

- (void)setHeadDic:(NSDictionary *)headDic {
    if (_headDic != headDic) {
        _headDic = headDic;
    }
    [self.upLabel setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:headDic[@"img"]] placeholderImage:[UIImage imageNamed:@"nopic"]];
}


- (void)setLeftModel:(ZGLOptionsModel *)leftModel {
    if (_leftModel != leftModel) {
        _leftModel = leftModel;
    }
    __weak typeof(self) weakSelf = self;
    self.leftLabel.tintColor = artiColor;
    self.titleLabel.textColor = artiColor;
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:leftModel.large_pic]];
    [self.leftLabel setImageForState:(UIControlStateNormal) withURLRequest:request placeholderImage:[UIImage imageNamed:@"nopic"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        [weakSelf.leftLabel setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:(UIControlStateNormal)];
        weakSelf.titleLabel.text = leftModel.title;
    } failure:^(NSError *error) {
        
    }];
    


}
- (void)setRightModel:(ZGLOptionsModel *)rightModel {
    if (_rightModel != rightModel) {
        _rightModel = rightModel;
    }
    __weak typeof(self) weakSelf = self;
    self.rightTitleLabel.textColor = artiColor;
    [self.rightLabel setTitle:rightModel.title forState:(UIControlStateNormal)];
    [self.rightLabel setTitleColor:[self getColor:rightModel.block_color] forState:(UIControlStateNormal)];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:rightModel.large_pic]];
    [self.rightLabel setImageForState:(UIControlStateNormal) withURLRequest:request placeholderImage:[UIImage imageNamed:@"nopic"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        [weakSelf.rightLabel setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:(UIControlStateNormal)];
        weakSelf.rightTitleLabel.text = rightModel.title;
    } failure:^(NSError *error) {
        
    }];

}


- (UIColor *)getColor:(NSString *)hexColor
{
    NSString *string = [hexColor substringFromIndex:1];//去掉#号
    unsigned int red,green,blue;
    NSRange range;
    range.length = 2;
    
    range.location = 0;
    red = [self stringToInt:[string substringWithRange:range]];
    
    range.location = 2;
    green = [self stringToInt:[string substringWithRange:range]];
    
    range.location = 4;
    blue = [self stringToInt:[string substringWithRange:range]];
    
    return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green / 255.0f) blue:(float)(blue / 255.0f) alpha:1.0f];
}

- (int)stringToInt:(NSString *)string{
    
    unichar hex_char1 = [string characterAtIndex:0]; ////两位16进制数中的第一位(高位*16)
    int int_ch1;
    if(hex_char1 >= '0' && hex_char1 <='9')
        int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
    else if(hex_char1 >= 'A' && hex_char1 <='F')
        int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
    else
        int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97
    
    unichar hex_char2 = [string characterAtIndex:1]; ///两位16进制数中的第二位(低位)
    int int_ch2;
    if(hex_char2 >= '0' && hex_char2 <='9')
        int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
    else if(hex_char1 >= 'A' && hex_char1 <='F')
        int_ch2 = hex_char2-55; //// A 的Ascll - 65
    else
        int_ch2 = hex_char2-87; //// a 的Ascll - 97
    return int_ch1+int_ch2;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
